package team.scau.laboratory_repair.web.pc;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.common.json.PoToDto;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.common.utils.JsonTool;
import team.scau.laboratory_repair.modules.dto.DomainDto;
import team.scau.laboratory_repair.modules.po.Area;
import team.scau.laboratory_repair.modules.po.Domain;
import team.scau.laboratory_repair.modules.po.QDomain;
import team.scau.laboratory_repair.service.AreaService;
import team.scau.laboratory_repair.service.DomainService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:
 * @Author:ZTF
 * @Date:2018/5/23 13:46
 */
@Controller
@CommonsLog
@RequestMapping(value="/pc/biz/domain")
public class DomainController extends BaseController {

    @Autowired
    private DomainService domainService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private PoToDto poToDto;

    @ModelAttribute("domain")
    public Domain get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return domainService.findById(id);
        } else {
            return new Domain();
        }
    }

    @RequiresPermissions("biz:domain:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        List<Area> areas = areaService.listOrByExample(null, null).getResults();
        model.addAttribute("areas", areas);
        return "/pc/biz/domain/list";
    }

    @ResponseBody
    @RequiresPermissions("biz:domain:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list(@ModelAttribute Domain domain) throws Exception {
        MyPage page = getPage(QDomain.class);
        QueryResults<Domain> list = domainService.listOrByExample(domain, page);
        List<DomainDto> dtos = poToDto.domainDtoList(list.getResults());
        return new JqGridResult(dtos);
    }

    @RequiresPermissions("biz:domain:edit")
    @GetMapping(value = "/edit", produces = "text/html;charset=utf-8")
    public String edit(@ModelAttribute("domain") Domain domain, Model model) throws Exception{
        model.addAttribute("areas", areaService.findAll());
        model.addAttribute("domain",domain);
        model.addAttribute("staffTree", JsonTool.toJson(domainService.selectStaffTree(domain)));
        return "/pc/biz/domain/edit";
    }

    @RequiresPermissions("biz:domain:edit")
    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Domain domain){
        Map<String,Object> result = new HashMap<>();
        try {
            String[] strings = domain.getRoom().split(",");
            if(strings.length > 1){
                for(String s : strings){
                    Domain d = new Domain(domain);
                    d.setRoom(s);
                    domainService.save(d);
                }
                result.put("msg","配置批量保存成功");
            } else {
                domainService.save(domain);
                result.put("msg","配置保存成功");
            }
            result.put("status",200);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","配置保存失败");
        }
        return result;
    }
    @RequiresPermissions("biz:domain:delete")
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap<>();
        try {
            if(id != null && id > 0){
                domainService.deleteById(id);
                result.put("status",200);
                result.put("msg","删除房间配置成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的配置");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","删除配置失败");
        }
        return result;
    }
}
